/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import util.Utils;

/**
 *
 * @author thanhle
 */
public class SessionTimeoutFilter extends LoginBacking implements Filter {

    private static final String LOGIN_PAGE = "/index.xhtml";
    private String timeoutPage = "/timeout.xhtml";
    private String welcomePage = "/index.xhtml";
    FilterConfig config = null;
    ServletContext servletContext = null;

    public void init(FilterConfig filterConfig) throws ServletException {
        config = filterConfig;
        servletContext = config.getServletContext();
    }

    public void doFilter(ServletRequest request,
            ServletResponse response, FilterChain filterChain) throws IOException, ServletException {

        if ((request instanceof HttpServletRequest)
                && (response instanceof HttpServletResponse)) {

            HttpServletRequest httpServletRequest = (HttpServletRequest) request;
            HttpServletResponse httpServletResponse = (HttpServletResponse) response;
            // is session expire control required for this request?  
            if (checkResource(httpServletRequest, httpServletRequest.getContextPath() + "/")) {

                // is session invalid?  
//                System.out.println("Session invalid: " + isSessionInvalid(httpServletRequest));
                if (isSessionInvalid(httpServletRequest)) {
                    Utils.log(servletContext, "redirecting to " + httpServletRequest.getContextPath()
                            + "/" + timeoutPage);

//                    preparelogout();
                    httpServletResponse.sendRedirect(httpServletRequest.getContextPath() + LOGIN_PAGE);
                    return;

                }
            }
        }
        filterChain.doFilter(request, response);
    }

    private boolean isSessionControlRequiredForThisResource(
            HttpServletRequest httpServletRequest) {
        String requestPath = httpServletRequest.getServletPath();
        boolean targetIsTimeoutPage = !(requestPath.contains(timeoutPage));
        //   boolean targetIsWelcomePage = !(requestPath.contains(welcomePage));
        boolean controlRequired = (targetIsTimeoutPage);
        return controlRequired;
    }

    private boolean checkResource(HttpServletRequest request, String path) {
        String requestPath = request.getRequestURI();

        return !(requestPath.indexOf(timeoutPage) > 0 || requestPath.indexOf(welcomePage) > 0 || requestPath.equals(path));
    }

    private boolean isSessionInvalid(HttpServletRequest httpServletRequest) {

        return httpServletRequest.getRequestedSessionId() != null && !httpServletRequest.isRequestedSessionIdValid();

    }

    public void destroy() {
    }

    public String getTimeoutPage() {

        return timeoutPage;

    }

    public void setTimeoutPage(String timeoutPage) {

        this.timeoutPage = timeoutPage;

    }
}
